<template>
    <el-dialog v-model="centerDialogVisibleFlag" title="提醒" style="width: 500px;" align-center>
        <div class="dialog_content_box">
            <el-icon>
                <WarningFilled />
            </el-icon>
            <p>确认要更新到资产明细吗？</p>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="centerDialogVisibleFlag = false, saveFileId = ''">取消</el-button>
                <el-button type="primary" @click="saveFileHandle" :disabled="flagTime > 0">
                    <span v-if="flagTime > 0">{{ flagTime }}s </span> 确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <addEditLookProperty ref="addEditLookProperty" />
    <addFileView ref="addFileView" />
    <addEditTaskListView ref="addEditTaskListView" />
    <addLookOtherProperty ref="addLookOtherProperty" />
    <el-main>
        <div class="teacher">
            <div class="app-table">
                <div class="app-body">
                    <div class="app-search">
                        <el-form :inline="true" class="demo-form-inline" v-model="form">
                            <el-form-item label="任务标题">
                                <el-input placeholder="请输入任务标题" v-model="addAssetsForm.title"
                                    @keyup.enter="searchEnter"></el-input>
                            </el-form-item>

                            <el-form-item>
                                <el-button class="serach_btn" type="primary" @click="onSubmit"
                                    @keyup.enter="searchEnter">搜索</el-button>
                                <el-button class="add_btn" type="success" @click="add">添加</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <el-table :data="list" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="id" label="id" width="60" />
                        <el-table-column prop="title" label="任务标题" width="280" />
                        <el-table-column prop="content" label="任务描述" width="280" />
                        <el-table-column prop="teacherNames" label="盘点人" width="200" />
                        <el-table-column prop="departmentName" label="盘点部门" width="110" />
                        <el-table-column prop="createName" label="创建⼈" />
                        <el-table-column prop="startTime" label="任务开始⽇期" width="150" />
                        <el-table-column prop="entTime" label="任务结束⽇期" width="150" />

                        <el-table-column label="校区">
                            <template style="display: block" #default="scope">
                                <span v-if="scope.row.campus === 1">校本部</span>
                                <span v-if="scope.row.campus === 2">东校区</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="400px" fixed="right">
                            <template style="display: block" #default="scope">
                                <div style="display: flex;">
                                    <div>
                                        <el-button class="edit_btn" size="small" type="danger"
                                            @click="lookProperty(scope.row)">查看资产
                                        </el-button>
                                        <el-button class="edit_btn" size="small" type="danger"
                                            @click="lookOtherProperty(scope.row)">查看其他资产
                                        </el-button>
                                        <el-button class="edit_btn" size="small" type="danger"
                                            @click="saveFile(scope.row)">归档
                                        </el-button>
                                        <el-button class="edit_btn" size="small" type="success"
                                            @click="editAssetsDetail(scope.row)">编辑
                                        </el-button>
                                        <el-button class="del_btn" size="small" type="danger"
                                            @click="deleteAssetsDetail(scope.row.id)">删除
                                        </el-button>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <template v-slot:empty>
                            <el-empty description="暂无数据"></el-empty>
                        </template>
                    </el-table>
                    <el-config-provider :locale="locale">
                        <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                            layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                            @current-change="changeCurrentPage">
                        </el-pagination>
                    </el-config-provider>
                </div>
                <bottomView />
            </div>
        </div>
    </el-main>
</template>
<script>
import addLookOtherProperty from "@/components/assets/addLookOtherProperty.vue";
import addEditLookProperty from "@/components/assets/addEditLookProperty.vue";
import addEditFaAssetsView from "@/components/assets/addEditFaAssets.vue";
import addEditTaskListView from "@/components/assets/addEditTaskList.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload, WarningFilled } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";
import { API_BASE_NAME2, API_BASE_NAME3 } from '../../constants';

export default {
    name: "FaProjectListView",
    data() {
        return {
            campusName: API_BASE_NAME2,
            campusName1: API_BASE_NAME3,
            type: 0,
            title: "",
            page: 1,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            centerDialogVisibleFlag: false,
            flagTime: 15,
            saveFileId: "",
            allTeacher: [],
            addAssetsForm: {
                title: "",
                content: "",
                createName: "",
                createNumber: "",
                createTime: "",
                startTime: "",
                entTime: "",
                campus: "",
            },
            user: {},
            roleName: "",
        };
    },
    components: {
        // addEditFaAssetsAloneView,
        // addEditChaiFenView,
        addEditTaskListView,
        addEditFaAssetsView,
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload,
        bottomView, addEditLookProperty, WarningFilled, addLookOtherProperty
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.user = JSON.parse(sessionStorage.getItem('user'))
        this.getMyPermission();
        // this.getFaTaskList();
    },
    watch: {
        // $route() {
        //     this.getFaTaskList();
        // },
    },
    methods: {

        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getFaTaskList();
            }
        },

        getMyPermission() {
            httpService("/javaApi/getMyPermission", {}, "get").then((data) => {
                if (data) {
                    console.log(123, data.includes('assetsManager'), data.includes('assetsLaboratory'));
                    // data.includes(assetsManager)
                    if (data.includes('assetsManager') && data.includes('assetsLaboratory')) {
                        this.roleName = '普通权限'
                    } else {
                        this.roleName = '管理员'
                    }
                    console.log(this.roleName);
                    this.getFaTaskList();
                }
            });
        },

        add() {
            this.$refs.addEditTaskListView.title = "发起任务"
            this.$refs.addEditTaskListView.personnelType = this.type
            this.$refs.addEditTaskListView.dialogFormVisible = true;
            this.$refs.addEditTaskListView.allTeacherList = this.allTeacher;
            this.$refs.addEditTaskListView.getDepartmentList()
            // this.$refs.addEditTaskListView.getBaseTeacherList()
            // this.$refs.addEditTaskListView.getCurrentQuarter()
            // this.$refs.addEditTaskListView.getDepartmentList1()
            this.$refs.addEditTaskListView.getListmy()
        },

        editAssetsDetail(obj) {
            this.$refs.addEditTaskListView.title = "修改任务"
            this.$refs.addEditTaskListView.personnelType = this.type
            this.$refs.addEditTaskListView.dialogFormVisible = true;
            this.$refs.addEditTaskListView.getListmy()
            this.$refs.addEditTaskListView.getTaskById(obj.id)
            this.$refs.addEditTaskListView.id = obj.id;
            this.$refs.addEditTaskListView.getDepartmentList()
            this.$refs.addEditTaskListView.getDepartmentList1()
            this.$refs.addEditTaskListView.getBaseTeacherList()
        },
        lookProperty(obj) {
            this.$refs.addEditLookProperty.title = "盘点资产列表"
            this.$refs.addEditLookProperty.personnelType = this.type
            this.$refs.addEditLookProperty.dialogFormVisible = true;
            this.$refs.addEditLookProperty.id = obj.id;
            this.$refs.addEditLookProperty.getInventoryList(obj.id)
            this.$refs.addEditLookProperty.getInventoryList1(obj.id)
            this.$refs.addEditLookProperty.getBaseTeacherList()
            this.$refs.addEditLookProperty.getAddressList()
            this.$refs.addEditLookProperty.getDepartmentList()
            this.$refs.addEditLookProperty.getDepartmentAllList()
        },
        lookOtherProperty(obj) {
            this.$refs.addLookOtherProperty.title = "手动添加资产"
            // this.$refs.addLookOtherProperty.personnelType = this.type
            this.$refs.addLookOtherProperty.dialogFormVisible = true;
            this.$refs.addLookOtherProperty.id = obj.id;
            this.$refs.addLookOtherProperty.projectId = obj.id;
            this.$refs.addLookOtherProperty.getInventoryList()
        },

        deleteAssetsDetail(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/faTask/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getFaTaskList();
                            } else {
                                ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        exportHandle() {
            httpService("/javaApi/faTask/exportExcel", {
                title: this.addAssetsForm.title.trim(),
            }, "get").then(
                (data) => {
                    console.log(data);
                    if (data.code == 200) {
                        // "http://192.168.9.20:8088" +
                        window.open(data.message, '_blank')
                    }
                }
            );
        },
        handleChange(e) {
            let file = e.target.files[0]
            let params = new FormData()
            params.append('file', file)
            httpService("/javaApi/faTask/importExcel", params, "post").then(
                (data) => {
                    console.log(data);
                    if (data.code == 200) {
                        ElMessage.success(data.object);
                        // this.importPreview()
                    } else {
                        ElMessage.error(data.object);
                    }
                }
            );
        },
        reloadData() {
            this.page = 1;
            this.getFaTaskList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入资产项目表";
            this.$refs.addFileView.uploadApiUrl = "/api/faProject/importExcel";
        },

        onSubmit() {
            this.getFaTaskList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getFaTaskList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getFaTaskList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getFaTaskList() {
            let obj = {}
            if (this.roleName == '管理员') {
                obj = { "title": this.addAssetsForm.title }
            } else if (this.roleName == '普通权限') {
                obj = { "title": this.addAssetsForm.title, "createNumber": this.user.teacherNumber }
            }
            httpService(
                "/javaApi/faTask/list/" + this.page + "/" + this.pageSize,
                obj,
                "get"
            ).then((data) => {
                if (data.list) {
                    data.list = data.list.map(item => {
                        if (item.startTime && item.entTime) {
                            return {
                                ...item,
                                startTime: item.startTime.slice(0, 10),
                                entTime: item.entTime.slice(0, 10),
                            }
                        } else {
                            return {
                                ...item,
                            }
                        }
                    })
                }
                this.list = data.list;
                this.total = data.total;
            });
        },

        saveFile(data) {
            this.saveFileId = data.id
            this.flagTime = 15
            this.centerDialogVisibleFlag = true
            let timer = setInterval(() => {
                if (this.flagTime == 0) {
                    clearInterval(timer)
                    // this.flagTime = 15
                } else {
                    this.flagTime -= 1
                }
            }, 1000)
        },
        saveFileHandle() {
            let params = new FormData()
            params.append('taskId', this.saveFileId)
            httpService(
                "/javaApi/faSubList/invokeSync", params, "post"
            ).then((data) => {
                this.saveFileId = ''
                this.centerDialogVisibleFlag = false
                if (data.code == 200) {
                    ElMessage.success("同步成功");
                } else {
                    ElMessage.warning("同步失败");
                }
            })
        }
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}



.app-table {
    background: #eff3f5;
    padding: 10px;
}

.app_add {
    background-color: #fff;
    margin-bottom: 10px;
    padding: 10px;
}

.app-search {
    /* padding: 10px; */
    background-color: #fff !important;
}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.app-body {
    background: white;
    padding: 20px 30px 10px 20px;
    margin-bottom: 10px;
    width: calc(100% - 50px);
    min-height: calc(100vh - 244px);
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    padding: 20px 10px;
    background-color: #fff;
}

.serach_btn {
    background-color: #007EFF;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.add_btn {
    background-color: #00c78c;
    color: #fff;
    border-radius: 8px;
    border: none;
}

.export_btn {
    background-color: #FF7200;
    border-radius: 8px;
    color: #fff;
    border: none;
}

.edit_btn {
    background-color: #fff;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: #fff;
    color: #FF0000;
    border: none;
}

.el-dialog .el-dialog__body {
    padding: 0px 20px !important;
    /* --el-dialog-padding-primary: 0px; */
}

.el-icon {
    color: rgb(230, 162, 60);
    font-size: 26px;
}

.dialog_content_box {
    display: flex;
    align-items: center;
}

.dialog_content_box p {
    margin: 0 0 0 20px;
}

.dialog-footer button {
    width: 100px;
}
</style>
